﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinFormsMvp;
using EngineLayer;
using System.Windows.Forms;
using PresentationLayer.Views.ViewContracts;
using PresentationLayer.Presenters;
using WinFormsMvp.Forms;
using PresentationLayer.Properties;

namespace PresentationLayer.Views
{  
    [PresenterBinding(typeof(LoginPresenter))]
    public class LoginForm : MvpForm<LoginModel>, ILoginView
    {
        #region PrivateDeclarations 
            private Button btnAccept;
            private Button btnCancel;
            private Panel panel1;
            private TextBox txtPassword;
            private TextBox txtLoginID;
            private Label lblPassword;
            private Label lblUser;
            private PictureBox pictureBox1;
            private Label label1;
        #endregion
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.btnAccept = new Button();
            this.btnCancel = new Button();
            this.panel1 = new Panel();
            this.txtPassword = new TextBox();
            this.txtLoginID = new TextBox();
            this.lblPassword = new Label();
            this.lblUser = new Label();
            this.pictureBox1 = new PictureBox();
            this.label1 = new Label();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // btnAceptar
            // 
            this.btnAccept.CausesValidation = false;
            this.btnAccept.Location = new System.Drawing.Point(282, 140);
            this.btnAccept.Name = "btnAceptar";
            this.btnAccept.Size = new System.Drawing.Size(75, 23);
            this.btnAccept.TabIndex = 2;
            this.btnAccept.Text = Resources.Aceptar;
            this.btnAccept.UseVisualStyleBackColor = true;
            this.btnAccept.Click += new System.EventHandler(this.OnClickButton);
              // 
            // btnCancelar
            // 
            this.btnCancel.CausesValidation = false;
            this.btnCancel.Location = new System.Drawing.Point(200, 140);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 100;
            this.btnCancel.Text = Resources.Cancelar;
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.OnClickButton);
            // 
            // panel1
            // 
            this.panel1.BorderStyle = BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.txtPassword);
            this.panel1.Controls.Add(this.txtLoginID);
            this.panel1.Controls.Add(this.lblPassword);
            this.panel1.Controls.Add(this.lblUser);
            this.panel1.Controls.Add(this.pictureBox1);
            this.panel1.Location = new System.Drawing.Point(12, 12);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(346, 122);
            this.panel1.TabIndex = 0;
            // 
            // txtPassword
            // 
            this.txtPassword.Location = new System.Drawing.Point(194, 62);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.PasswordChar = '*';
            this.txtPassword.Size = new System.Drawing.Size(142, 20);
            this.txtPassword.TabIndex = 3;
            this.txtPassword.LostFocus += new EventHandler(TxtPasswordLostFocus);
            this.txtPassword.KeyDown += new KeyEventHandler(this.OnKeyDownInTextBox);
            // 
            // txtLoginID
            // 
            this.txtLoginID.Location = new System.Drawing.Point(194, 36);
            this.txtLoginID.Name = "txtLoginID";
            this.txtLoginID.Size = new System.Drawing.Size(142, 20);
            this.txtLoginID.TabIndex = 1;
            this.txtLoginID.LostFocus += new EventHandler(TxtLoginIDLostFocus);
            this.txtLoginID.KeyDown+= new KeyEventHandler(this.OnKeyDownInTextBox);
            // 
            // lblPassword
            // 
            this.lblPassword.AutoSize = true;
            this.lblPassword.Location = new System.Drawing.Point(124, 65);
            this.lblPassword.Name = "lblPassword";
            this.lblPassword.Size = new System.Drawing.Size(63, 13);
            this.lblPassword.TabIndex = 2;
            this.lblPassword.Text = Resources.Contrasena;
            // 
            // lblUser
            // 
            this.lblUser.AutoSize = true;
            this.lblUser.Location = new System.Drawing.Point(124, 39);
            this.lblUser.Name = "lblUser";
            this.lblUser.Size = new System.Drawing.Size(39, 13);
            this.lblUser.TabIndex = 0;
            this.lblUser.Text = Resources.Usuario;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = Resources.logo;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(122, 121);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 6;
            this.pictureBox1.TabStop = false;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 149);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(0, 13);
            this.label1.TabIndex = 1;
            // 
            // frmLogin
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(371, 169);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.btnAccept);
            this.Controls.Add(this.btnCancel);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "LoginForm";
            this.ShowIcon = false;
            this.StartPosition = FormStartPosition.CenterScreen;
         //   this.Load += new System.EventHandler(this.frmLogin_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void OnClickButton(object sender, EventArgs e)
        {
           if(((Button)sender).Text == Resources.Cancelar)
                CloseFormClicked(null, EventArgs.Empty);
           else if(((Button)sender).Text == Resources.Aceptar)
                ProcessLogin(null, EventArgs.Empty);
        }
      
        private void OnKeyDownInTextBox(object sender, KeyEventArgs e)
        { 
            if (e.KeyCode == Keys.Enter)
            {
                if(txtLoginID.Focused)
                    txtPassword.Focus();
                else if (txtPassword.Focused)
                {
                    txtLoginID.Focus();
                    ProcessLogin(null, EventArgs.Empty);
                }
            }
        } 
      
        #region Implementation of ILoginView

        public event EventHandler CloseFormClicked;
        public event EventHandler ProcessLogin;
        public event EventHandler TxtLoginIDLostFocus;
        public event EventHandler TxtPasswordLostFocus;
       
        public void CloseForm()
        {
            this.Close();
        }

        #endregion

    }
}
